<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>品类元数据</title>
	<link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
	<link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
	<link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
	<link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
	<script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
	<script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
	<script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
	<script type="text/javascript" src="/webapp/js/common/config.js"></script>
</head>
<body>
<div class="easyui-container-fluid">
	<div class="easyui-panel" style="border: none;">
		<div class="nav" id="nav"></div>
		<div class="easyui-panel" style="border:none;">
			<form id="ff" method="post">
				<div style="margin-bottom:20px">
					<label class="item-label">品类：</label>
					<select class="easyui-combobox" id="categorySelect" name="categoryCode" style="width:174px;" required="required"   data-options="multiple:true">
					</select>
				</div>
				<div style="margin-bottom:20px">
					<label class="item-label" ><span class="item-required">*</span>名称：</label>
					<input class="easyui-textbox" name="name" style="width:174px;" data-options="required:true">
				</div>
				<div style="margin-bottom:20px">
					<label class="item-label">数据类型：</label>
					<select class="easyui-combobox" id="type" name="type" data-options="panelHeight:'auto'" style="width:174px;" required="required" >
						<option value="INPUT">文字</option>
						<!--<option value="BOOLEAN">布尔</option>-->
						<option value="NUMBER">数字</option>
						<option value="SELECTED">单选</option>
						<option value="MULTI">多选</option>
					</select>
					<span id="numberScaleSpan" style="display: none;">
								<label class="item-label" >数字精度：</label>
								<input data-options="width:40,min:0,max:2" class="easyui-numberbox" type="text" name="scale" value="0" style="text-align: center;"/>(小数点后保留几位小数。0表示整数，1表示一位小数(例如1.6或者1.0))
							</span>
				</div>
				<div style="margin-bottom:20px">
					<label class="item-label" >可选值：</label>
					<input class="easyui-tagbox" name="values" style="width:50%;min-height:60px;" data-options="multiline:true">
				</div>
				<div style="margin-bottom:20px;">
					<label class="item-label" style="display:inline-block;">是否必填：</label>
					<input id="require_yes" name="require" value="true" type="radio"/>
					<label for="require_yes">是</label>
					<input id="require_no" name="require" value="false" type="radio" checked="checked"/>
					<label for="require_no">否</label>
				</div>
				<div style="margin-bottom:20px;">
					<label class="item-label" style="display:inline-block;">列表展示页：</label>
					<input id="visible_yes" name="visibleOnList" value="true" type="radio"/>
					<label for="visible_yes">是</label>
					<input id="visible_no" name="visibleOnList" value="false" type="radio" checked="checked"/>
					<label for="visible_no">否</label>
				</div>
				<div style="margin-bottom:20px;">
					<label class="item-label" style="display:inline-block;">一级规格：</label>
					<input id="fisrtLevel_yes" name="fisrtLevelMetadata" value="true" type="radio"/>
					<label for="fisrtLevel_yes">是</label>
					<input id="fisrtLevel_no" name="fisrtLevelMetadata" value="false" type="radio" checked="checked"/>
					<label for="fisrtLevel_no">否</label>
				</div>
				<div style="margin-bottom:20px;">
					<label class="item-label" style="display:inline-block;">工作台位置：</label>
					<input type="text" class="easyui-numberspinner " name="mainMetadata"  style="width:174px;" />

				</div>
				<div style="margin-bottom:20px" id="spliceIndexDiv">
					<label class="item-label" >拼接坐标：</label>
					<input class="easyui-textbox" name="spliceIndex" id="spliceIndex" style="width:174px;">
				</div>
				<div style="margin-bottom:20px" >
					<label class="item-label" >默认选中：</label>
					<input class="easyui-textbox" name="defaults" style="width:174px;">
				</div>
				<!--<div style="margin-bottom:20px">-->
				<!--<label class="item-label" >规格位置：</label>-->
				<!--<input class="easyui-textbox" name="position" style="width:174px;">-->
				<!--</div>-->
				<div style="margin-bottom:20px">
				<label class="item-label" >规格位置：</label>
				<input class="easyui-textbox" name="coordinateX" id ="x" style="width:85px;" >--
				<input class="easyui-textbox" name="coordinateY" id="y" style="width:85px;" >
				<div style="margin-top:20px">
					<label class="item-label" >识别列表：</label>
					<input class="easyui-tagbox" name="analyzeTexts" style="width:50%;min-height:60px;" data-options="multiline:true">
				</div>
				<div style="margin-bottom:20px" >
					<label class="item-label" >规格排除：</label>
					<input class="easyui-textbox" name="propExclude" style="width:274px;">
				</div>
			</div>

			</form>
			<div style="margin-left: 100px;margin-bottom: 20px;">
				<a href="javascript:void(0)" class="easyui-linkbutton c8" onclick="submitForm()" style="width:80px">保存</a>
				<a href="javascript:void(0)" class="easyui-linkbutton" onclick="cancelForm()" style="width:80px">取消</a>
				<a href="javascript:void(0)" class="easyui-linkbutton c5" onclick="updateAll()" style="width:160px ;margin-left: 50px;"> &nbsp &nbsp &nbsp 批量修改 &nbsp &nbsp&nbsp &nbsp&nbsp &nbsp（根据品类和名称）</a>
			</div>
		</div>

	</div>
</div>
</body>
<script type="text/javascript">
    var id = getQueryParameter("id"),categoryCode=getQueryParameter("categoryCode");
    var categoryType;
    $(function(){
        $('#x').textbox({
            prompt: '坐标X'
        });
        $('#y').textbox({
            prompt: '坐标y'
        });

        setNav();
        getCategoryType();
        $.when(initCategorySelect("categorySelect")).done(function(){
            initFormData();
        });
        $("#type").combobox({
            onChange: function (n, o) {
                if('NUMBER'==n){
                    $("#numberScaleSpan").show();
                }else{
                    $("#numberScaleSpan").hide();
                }
            }
        });
    });
    function initFormData(){
        if(id){
            // 编辑的时候，数据类型不能修改
            $("#type").combobox('readonly', true);
            $.ajax({
                url:config.url +"/categoryMetadata/"+id,
                type:'get',
                data:{},
                xhrFields:{withCredentials:true},
                success:function(data){
                    //填充表单
                    if(data){
                        var item = changeProperty2String(data);
                        if(!item.values){
                            item.values = [];
                        }
                        if (!item.analyzeTexts) {
                            item.analyzeTexts = [];
                        }
                        $("#ff").form("load",item);
                        if('NUMBER'==item['type']){
                            $("#numberScaleSpan").show();
                        }else{
                            $("#numberScaleSpan").hide();
                        }

                        if ($("#spliceIndex").textbox("getValue")) {
                            $("#spliceIndex").textbox("readonly", true);
                        }
                    }
                },
                error:function(){
                }
            });
        }
    }

    function getCategoryType() {
        $.ajax({
            url: config.url + "/category/get",
            type: "get",
            data: {code:categoryCode},
            xhrFields: {withCredentials: true},
            success: function (data) {
                if (data.code == 200) {
                    categoryType = data.msg.type;
                    if (categoryType == 2) {
                        $("#spliceIndexDiv").show();
                    }else {
                        $("#spliceIndexDiv").hide();
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            }
        });
    }
    function submitForm(){
        $("#ff").form("submit",{
            onSubmit:function(){
                var valid = $(this).form('enableValidation').form('validate');
                if(valid){
                    var formData = getFormData(document.getElementById("ff"));
                    console.log(formData.categoryCode);
                    if( typeof formData['categoryCode'] != 'string'){
                        $.messager.alert("提示","品类不能多个","info")
                        return;
                    }
                    var method = "POST";
                    if(id){
                        method = "PATCH";
                        formData.id = id;
                    }
                    if(formData['values'] && typeof formData['values'] === 'string'){
                        formData['values'] = [formData['values']]
                    }
                    if (formData['analyzeTexts']) {
                        if (typeof formData['analyzeTexts'] === 'string') {
                            formData['analyzeTexts'] = [formData['analyzeTexts']];
                        }
                    } else {
                        formData['analyzeTexts'] = [];
                    }

                    $.ajax({
                        url:config.url+"/categoryMetadata",
                        type:method,
                        data:JSON.stringify(formData),
                        dataType:"json",
                        contentType:"application/json;charset=UTF-8",
                        xhrFields:{withCredentials:true},
                        beforeSend:function(){
                            $.messager.progress({
                                title:"请稍等",
                                interval:50,
                                text:"操作进行中..."
                            });
                        },
                        complete:function(){
                            $.messager.progress("close");
                        },
                        success:function(data){
                            if (data.code == 515) {
                                $.messager.alert("提示", data.msg, "error");
                                return;
                            }
                            if(data){
                                $.messager.alert("提示","操作成功","info",function () {
                                    window.parent.closeCurrentTab(true);
                                });
                            }
                        },
                        error:function(data){
                            if(data){
                                $.messager.alert("提示",data.responseJSON.message||"操作失败","error");
                            }
                        }
                    });
                    return false;
                }
                return false;
            }
        })
    }
    function updateAll(){
        $("#ff").form("submit",{
            onSubmit:function(){
                var valid = $(this).form('enableValidation').form('validate');
                if (!valid) {
                    return false;
                } else {
                    var formData = getFormData(document.getElementById("ff"));
                    var method = "POST";
                    if (id) {
                        method = "PATCH";
                        formData.id = id;
                    }
                    if (formData['categoryCode'] && typeof formData['categoryCode'] === 'string') {
                        formData['cvalues'] = [formData['categoryCode']]
                    }else{
                        formData['cvalues'] = formData['categoryCode'];
                        formData['categoryCode'] = formData['categoryCode'][0];
                    }

                    if (formData['values'] && typeof formData['values'] === 'string') {
                        formData['values'] = [formData['values']]
                    }
                    $.ajax({
                        url: config.url + "/categoryMetadata/nameUpdate",
                        type: method,
                        data: JSON.stringify(formData),
                        dataType: "json",
                        contentType: "application/json;charset=UTF-8",
                        xhrFields: {withCredentials: true},
                        beforeSend: function () {
                            $.messager.progress({
                                title: "请稍等",
                                interval: 50,
                                text: "操作进行中..."
                            });
                        },
                        complete: function (data) {
                            // alert(data.responseJSON.code)
                            $.messager.progress("close");
                            if (data.responseJSON.code == 200) {
                                $.messager.alert("提示", data.responseJSON.msg, "info", function () {
                                    window.parent.closeCurrentTab(true);
                                });
                            }else {
                                $.messager.alert("提示", data.responseJSON.msg, "error");
                            }
                        },
                        success: function (data) {
                            /*if (data.responseText.code == 200) {
                                $.messager.alert("提示", data.responseText.msg, "info", function () {
                                    window.parent.closeCurrentTab(true);
                                });
                            }else {
                                $.messager.alert("提示", data.responseText.msg, "error");
                            }*/
                        },
                        error: function (data) {
                            if (data) {
                                $.messager.alert("提示", data.responseText.msg || "操作失败", "error");
                            }
                        }
                    });
                    return false;
                }
            }
        })
    }
    // function cancelForm(){
		// window.location.href="/webapp/pages/product/categoryMetadata/list.html";
    // }
    function cancelForm(){
        window.parent.closeCurrentTab();
    }
</script>
</html>